import { randomUUID } from "@/utils/index";
import { getFieldConfig } from "@/components/RuntimeComp";

const fieldConfig = getFieldConfig('Element')
const keyPrefix = "formKey_";

const labelMap = {
  备注: "textarea",
  费: "input-number",
  状态: "select",
  类: "select",
  类型: "select",
  时间: "range-picker",
  日期: "range-picker",
};

export function fieldFactory(type, label, formKey, smartType = true) {
  let nType = type;
  if (smartType && label) {
    for (const key of Object.keys(labelMap)) {
      if (label.endsWith(key)) {
        nType = labelMap[key];
      }
    }
  }
  return {
    type: nType,
    label: label ?? "字段_" + randomUUID(),
    formKey: formKey ?? keyPrefix + randomUUID(),
    props: { ...(fieldConfig[nType] ?? fieldConfig["input"]).props },
  };
}

export function columnFactory(title, dataIndex) {
  let column = {
    title,
    dataIndex: dataIndex ?? "col_" + randomUUID(),
    width: 200,
    hiddenInTable: false,
  };
  if (column.title === "序号") {
    column = {
      title: "序号",
      width: 70,
      customRender: ({ text, record, index }, _, v2Index) =>
        (v2Index || index) + 1,
    };
  }
  if (column.title === "操作") {
    column = {
      title: "操作",
      dataIndex: "action",
      fixed: "right",
      width: 150,
      scopedSlots: { customRender: "action" },
    };
  }
  column.queryConfig = {
    isQuery: false,
  };
  return column;
}
